package com.geekstudy.week3.controller;

import com.geekstudy.week3.service.MicroProfileRestClientService;
import com.geekstudy.week3.service.MockShutdownApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class MockActuatorShutdownController implements MockShutdownApi {

    final Logger logger = LoggerFactory.getLogger(MockActuatorShutdownController.class);

    @Autowired
    private MicroProfileRestClientService microProfileRestClientService;

    @RequestMapping("/demo")
    public Map<String, Map<String, String>> demo() {
        Map<String, Map<String, String>> map = microProfileRestClientService.postShutdown();
        return map;
    }

    // @RequestMapping(path = "/mock/shutdown", method = RequestMethod.POST)
    @Override
    public Map<String, String> shutdown(@RequestParam(name = "from") String from) {
        logger.info(" mockShutdown from:{} ", from);
        Map<String, String> resultMap = new LinkedHashMap<String, String>();

        resultMap.put("requestId", String.valueOf(System.nanoTime()));
        resultMap.put("from", from);
        resultMap.put("method", "api");
        resultMap.put("shutdown", "shutdown");

        return resultMap;
    }

    @RequestMapping(path = "/mock/actuator/shutdown", method = RequestMethod.POST)
    public Map<String, String> mockActuatorShutdown(@RequestParam(name = "from") String from) {
        logger.info(" mockActuatorShutdown from:{} ", from);
        Map<String, String> resultMap = new LinkedHashMap<String, String>();

        resultMap.put("requestId", String.valueOf(System.nanoTime()));
        resultMap.put("from", from);
        resultMap.put("method", "controller");

        return resultMap;
    }
}
